package com.graduation.sports.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.graduation.sports.base.BaseApplication;


/**
 * @ProjectName: attendance
 * @Package: com.graduation.attendance.util
 * @ClassName: UiUtil
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/2/4 10:28
 * @Version: 1.0
 */
public class UiUtil {
    public static final int REFRESHUI = 0x01;

    @SuppressLint("StaticFieldLeak")
    protected static Context mContext;

    public interface handlerListener {
        void refreshUI();
    }

    private static handlerListener mHandlerListener;

    protected static Handler sHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REFRESHUI:
                    if (mHandlerListener != null) {
                        mHandlerListener.refreshUI();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public static void init(BaseApplication application) {
        mContext = application;
    }

    public static Handler getHandler() {
        return sHandler;
    }

    public static Context getContext() {
        return mContext;
    }

    /**
     * 接受一个任务，并且在主线程中执行它
     */
    public static void post(Runnable task) {
        sHandler.post(task);
    }

    public static void post(Runnable task, long delay) {
        sHandler.postDelayed(task, delay);
    }

    public static void remove(Runnable task) {
        sHandler.removeCallbacks(task);
    }

}
